const { merge } = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const fs = require('fs');
const path = require('path');


const production = (common, appBuildInfo) => {
    const copyDirectorys = appBuildInfo.copyDirectory.split('\\');
    const plugins = [
        new MiniCssExtractPlugin({
            filename: "[name].[contenthash].css",
        })];
    if (fs.existsSync(appBuildInfo.copyDirectory)) {
        plugins.push(
            new CopyWebpackPlugin({
                patterns: [
                    {
                        from: appBuildInfo.copyDirectory,
                        to: appBuildInfo.pasteDirectory + '\\' + copyDirectorys[copyDirectorys.length - 1]
                    }
                ]
            })
        );
    }

    const config = merge(common, {
        mode: 'production',
        output: {
            filename: '[name].[contenthash].js',
            path: appBuildInfo.output,
            assetModuleFilename: (pathData) => {
                const pss = path.dirname(pathData.filename).split("/");
                const fps = [];
                let start = false;
                for (const ps of pss) {
                    if (ps === appBuildInfo.asset) start = true;
                    if (start) fps.push(ps);
                }
                const filepath = fps.join('/');
                return `${filepath}/[name].[hash][ext][query]`;
            },
            asyncChunks: true,
            clean: {
                keep: /^thumb|^reserve/,
            },
        },
        devtool: false,
        module: {
            rules: [],
        },
        plugins: plugins,
        optimization: {
            minimize: true,
            minimizer: [
                new TerserPlugin({ extractComments: false }),
            ],
        },
    });

    return config;
}


const development = (common, appBuildInfo) => {
    const copyDirectorys = appBuildInfo.copyDirectory.split('\\');
    const plugins = [
        new MiniCssExtractPlugin({
            filename: "[name].css",
        })];
    if (fs.existsSync(appBuildInfo.copyDirectory)) {
        plugins.push(
            new CopyWebpackPlugin({
                patterns: [
                    {
                        from: appBuildInfo.copyDirectory,
                        to: appBuildInfo.pasteDirectory + '\\' + copyDirectorys[copyDirectorys.length - 1]
                    }
                ]
            })
        );
    }

    const config = merge(common, {
        mode: 'development',
        output: {
            filename: '[name].js',
            path: appBuildInfo.output,
            assetModuleFilename: (pathData) => {
                const pss = path.dirname(pathData.filename).split("/");
                const fps = [];
                let start = false;
                for (const ps of pss) {
                    if (ps === appBuildInfo.asset) start = true;
                    if (start) fps.push(ps);
                }
                const filepath = fps.join('/');
                return `${filepath}/[name][ext][query]`;
            },
            asyncChunks: false,
            clean: {
                keep: /^thumb|^reserve/,
            },
        },
        devtool: 'inline-source-map',
        module: {
            rules: [],
        },
        plugins: plugins,
        devServer: {
            static: './',
            hot: true,
            compress: true,
            port: 3001,
            server: 'https',
        },
    });

    return config;
};



module.exports = {
    production,
    development
};